<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Think;
use Think\Auth;
class ResourceController extends Controller {
    /**
     * 资源列表页面
     * @author 田永强 loseboycn@qq.com
     */   
    function index(){
    	$resource = M('resource');
        $subject_section = M('subject_section');
    	$all_resource = $resource -> select();
        $all_subject_section = $subject_section -> select();
        foreach ($all_resource as &$unit) {
            foreach ($all_subject_section as $i) {
                if($unit['sectionid'] == $i['sectionid']){
                    $unit['sectionid'] = $i['chname'];
                }
            }
        }
        unset($unit); //取消引用

        $dict_resource = M('dict_resource');
        $dict_resource_result = $dict_resource -> select();
        foreach ($all_resource as &$unit) {
            foreach ($dict_resource_result as $i) {
                if($unit['rescode'] == $i['rescode']){
                    $unit['rescode'] = $i['resname'];
                }
            }
        }
        unset($unit); //取消引用

        $users = M('users');
        $users_result = $users -> select();
        foreach ($all_resource as &$unit) {
            foreach ($users_result as $i) {
                if($unit['id'] == $i['createuser']){
                    $unit['createuser'] = $i['username'];
                }
            }
        }
        unset($unit); //取消引用
        $this->assign('count',count($all_resource));
    	$this->assign('all_resource',$all_resource);
    	$this->display();
    }

    /**
     * 添加资源页面
     * @author 田永强 loseboycn@qq.com
     */   
    function resource_add(){
    	$dict_resource = M('dict_resource');
        $resource = M('resource');
        $subject = M('subject');
        $subject_section = M('subject_section');
        $subject_result = $subject -> select();
        $subject_section_result = $subject_section -> where('Sub_Code = "' . $subject_result[0]['sub_code'] .'"') -> select();
    	$all_dict_resource = $dict_resource -> select();
        $this->assign('subject_result',$subject_result);
        $this->assign('subject_section_result',$subject_section_result);
        $this->assign('all_dict_resource', $all_dict_resource);
    	$this->display();
    }

    /**
     * 资源审核通过
     * @author 田永强 loseboycn@qq.com
     */ 
    function verify_ok(){
        $resource = M('resource');
        $resource_info['status'] = 2;
        $result = $resource -> where('resid = "' . I('post.resid') . '"')-> save($resource_info);
        # 写入日志
        $resource_detail = M('resource_detail');

        $users = M('users');
        $operator = $users -> where('id = "' . session('id') . '"') -> getField('username');

        $log_info = array(
            'ResID'      => I('post.resid'),
            'operatTime'   => date("Y-m-d H:i:s",time()),
            'operate_Code' => 2,
            'describe'     => '审核通过',
            'operator'     => $operator,
        );
        $status = $resource_detail -> add($log_info);
        $this->ajaxReturn(true);
    }

    /**
     * 资源审核不通过
     * @author 田永强 loseboycn@qq.com
     */ 
    function verify_no(){
        $resource = M('resource');
        # 写入日志
        $resource_detail = M('resource_detail');

        $users = M('users');
        $operator = $users -> where('id = "' . session('id') . '"') -> getField('username');

        $log_info = array(
            'ResID'      => I('post.resid'),
            'operatTime'   => date("Y-m-d H:i:s",time()),
            'operate_Code' => 3,
            'describe'     => '审核未过',
            'operator'     => $operator,
        );
        $status = $resource_detail -> add($log_info);
        $resource_info['status'] = 3;
        $status = $resource -> where('resid = "' . I('post.resid') . '"')-> save($resource_info);
        $this->ajaxReturn(true);
    }

    /**
     * 资源审核页面
     * @author 田永强 loseboycn@qq.com
     */   
    function verify(){
        $resource = M('resource');
        $subject_section = M('subject_section');
        $all_resource = $resource -> where(' status = 1 ') -> select();
        $all_subject_section = $subject_section -> select();
        foreach ($all_resource as &$unit) {
            foreach ($all_subject_section as $i) {
                if($unit['sectionid'] == $i['sectionid']){
                    $unit['sectionid'] = $i['chname'];
                }
            }
        }
        unset($unit); //取消引用

        $dict_resource = M('dict_resource');
        $dict_resource_result = $dict_resource -> select();
        foreach ($all_resource as &$unit) {
            foreach ($dict_resource_result as $i) {
                if($unit['rescode'] == $i['rescode']){
                    $unit['rescode'] = $i['resname'];
                }
            }
        }
        unset($unit); //取消引用

        $users = M('users');
        $users_result = $users -> select();
        foreach ($all_resource as &$unit) {
            foreach ($users_result as $i) {
                if($unit['id'] == $i['createuser']){
                    $unit['createuser'] = $i['username'];
                }
            }
        }
        unset($unit); //取消引用
        $this->assign('count',count($all_resource));
        $this->assign('all_resource',$all_resource);
        $this->display();
    }

    /**
     * 添加资源方法
     * @author 田永强 loseboycn@qq.com
     */   
    function addresource(){
    	$resource = M('resource');
        $result = $resource -> where('resname = "' . I('post.resname') . '"') -> find();
        if($result){
        	$this->ajaxReturn('exist');
        }

    	$resource_info = array(
            'sectionID' => I('post.sectionid'),
    		'ResName' => I('post.resname'),
    		'ResCode' => I('post.rescode'),
    		'Intro' => I('post.intro'),
    		'lecturer' => I('post.lecturer'),
    		'status' => 1, //待审核
    		'CreateUser' => session('id'),
    		'CreateDate' => date("Y-m-d H:i:s",time()),
    	);

    	
        $file_info = $this -> upload();
        //文件上传异常检测
        if($file_info['status'] != 'success' ){
            $this->ajaxReturn($file_info['status']);
        }
        $resource_info['ResPath'] = $file_info['path'];
        
    	$result = $resource -> add($resource_info);
        if(!$result){
             $this->ajaxReturn('failed');
        }
        # 写入日志
        $resource_detail = M('resource_detail');

        $users = M('users');
        $operator = $users -> where('id = "' . session('id') . '"') -> getField('username');

        $log_info = array(
            'ResID'      => I('post.resid'),
            'operatTime'   => date("Y-m-d H:i:s",time()),
            'operate_Code' => 0,
            'describe'     => '保存',
            'operator'     => $operator,
        );
        $status = $resource_detail -> add($log_info);
        $this->ajaxReturn('success');
    }

    /**
     * 发布资源
     * @author 田永强 loseboycn@qq.com
     */   
    function startresource(){
        $resource = M('resource');
        $resource_info['status'] = 6;
        $result = $resource -> where('resid = "' . I('post.resid') . '"')-> save($resource_info);

        # 写入日志
        $resource_detail = M('resource_detail');

        $users = M('users');
        $operator = $users -> where('id = "' . session('id') . '"') -> getField('username');

        $log_info = array(
            'ResID'      => I('post.resid'),
            'operatTime'   => date("Y-m-d H:i:s",time()),
            'operate_Code' => 1,
            'describe'     => '发布',
            'operator'     => $operator,
        );
        $status = $resource_detail -> add($log_info);
        $this->ajaxReturn(true);
    }

    /**
     * 提交审核资源
     * @author 田永强 loseboycn@qq.com
     */   
    function submitresource(){
        $resource = M('resource');
        $resource_info['status'] = 1;
        $result = $resource -> where('resid = "' . I('post.resid') . '"')-> save($resource_info);
        $this->ajaxReturn(true);
    }

    /**
     * 删除资源
     * @author 田永强 loseboycn@qq.com
     */   
    function delresource(){
        $resource = M('resource');
        $path = $resource -> where('resid = "' . I('post.resid') . '"' ) -> getField('ResPath');
        unlink($path);//删除封面文件
        $result = $resource -> where('resid = "' . I('post.resid') . '"')-> delete();
        $this->ajaxReturn(true);
    }

    /**
     * 下线资源
     * @author 田永强 loseboycn@qq.com
     */   
    function stopresource(){
        $resource = M('resource');
        $resource_info['status'] = 5;
        $result = $resource -> where('resid = "' . I('post.resid') . '"')-> save($resource_info);
        # 写入日志
        $resource_detail = M('resource_detail');

        $users = M('users');
        $operator = $users -> where('id = "' . session('id') . '"') -> getField('username');

        $log_info = array(
            'ResID'      => I('post.resid'),
            'operatTime'   => date("Y-m-d H:i:s",time()),
            'operate_Code' => 5,
            'describe'     => '停用',
            'operator'     => $operator,
        );
        $status = $resource_detail -> add($log_info);
        $this->ajaxReturn(true);
    }

    /**
     * 二级联动，获取科目章节
     * @author 田永强 loseboycn@qq.com
     */   
    function getsection(){
        $subject_section = M('subject_section');
        $all_subject_section = $subject_section -> where('sub_code = "' . I('post.sub_code') . '"')-> select();
        $this->ajaxReturn($all_subject_section);
    }

    /**
     * 文件上传方法
     * @author 田永强 loseboycn@qq.com
     */  
    private function upload(){
        $config = array(
                'mimes'         =>  array(), //允许上传的文件MiMe类型
                'maxSize'       =>  0, //上传的文件大小限制 (0-不做限制)
                'exts'          =>  array('mp4', 'ppt','pdf'), //允许上传的文件后缀
                'autoSub'       =>  false, //自动子目录保存文件
                'subName'       =>  array('date', 'Y-m'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
                'rootPath'      =>  './Uploads/resource/', //保存根路径
                'savePath'      =>  '', //保存路径
                'saveName'      =>  array('uniqid',''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
                'saveExt'       =>  '', //文件保存后缀，空则使用原后缀
                'replace'       =>  false, //存在同名是否覆盖
                'hash'          =>  true, //是否生成hash编码
                'callback'      =>  false, //检测文件是否存在回调，如果存在返回文件信息数组
                'driver'        =>  '', // 文件上传驱动
                'driverConfig'  =>  array(), // 上传驱动配置
            );
        $upload = new \Think\Upload($config);// 实例化上传类
        $info   =   $upload->upload();
        if(!$info) {
        	//上传错误返回错误信息
            $file_info['status'] = $upload->getError();
            return $file_info;
        }else{
        	//上传成功
            $file_info = array(
                'path' => "Uploads/resource/" . $info['file']['savename'],
                'status' => "success",
                );
            return $file_info;
        }
    }

    /**
     * 资源日志页面
     * @author 田永强 loseboycn@qq.com
     */   
    function log(){
        $resource_detail = new \Admin\Model\View_resource_detailModel();
        $all_resource_log = $resource_detail -> select();

        $dict_resource = M('dict_resource');
        $all_dict_resource = $dict_resource -> select();

        $dict_operate = M('dict_operate');
        $all_dict_operate = $dict_operate -> select();

        $subject = M('subject');
        $all_subject = $subject -> select();

        $subject_section = M('subject_section');
        $all_subject_section = $subject_section -> where('sub_code = "' . $all_subject[0]['sub_code'] . '"' ) -> select();

        $dict_resource = M('dict_resource');
        $all_dict_resource = $dict_resource -> select();

        $users = M('users');
        $all_users = $users -> select();

        foreach ($all_resource_log as &$unit) {
            foreach ($all_dict_operate as $i) {
                if($unit['operate_code'] == $i['operate_code']){
                    $unit['operate_code'] = $i['operate_name'];
                }
            }

            foreach ($all_dict_resource as $i) {
                if($unit['rescode'] == $i['rescode']){
                    $unit['rescode'] = $i['resname'];
                }
            }
        }
        unset($unit); //取消引用

        $this->assign('all_resource_log', $all_resource_log);
        $this->assign('all_subject', $all_subject);
        $this->assign('all_subject_section', $all_subject_section);
        $this->assign('all_dict_resource', $all_dict_resource);
        $this->assign('all_users', $all_users);
        $this->display();
    }


    /**
     * 资源查询
     * @author 田永强 loseboycn@qq.com
     */   
    function search(){
        $sub_code = I('post.sub_code');
        $sectionid = I('post.sectionid');
        $rescode = I('post.rescode');
        $id = I('post.id');
        $logmin = I('post.logmin');
        $logmax = I('post.logmax');

        if($sub_code != '-1'){
            $map['Sub_Code']  = array('eq',$sub_code);
        }
        if($sectionid != '-1'){
            $map['sectionID']  = array('eq',$sectionid);
        }
        if($rescode != '-1'){
            $map['ResCode'] = array('eq',$rescode);
        }
        if($id != '全部人员'){
            $map['operator'] = array('eq',$id);
        }

        if($logmin != '' && $logmax != ''){
            $map['operatTime'] = array(array('EGT', $logmin), array('ELT', date("Y-m-d",strtotime("+1 day",strtotime($logmax)))));
        }

        else if($logmax != '' && $logmin == ''){
            $map['operatTime']  = array('ELT',date("Y-m-d",strtotime("+1 day",strtotime($logmax))));
        }
        else if($logmax == '' && $logmin != ''){    
            $map['operatTime']  = array('EGT',$logmin);
        }


        $resource_detail = new \Admin\Model\View_resource_detailModel();
        $search_detail_all = $resource_detail -> where($map) -> select();

        $dict_operate = M('dict_operate');
        $all_dict_operate = $dict_operate -> select();

        $dict_resource = M('dict_resource');
        $all_dict_resource = $dict_resource -> select();

        $users = M('users');
        $all_users = $users -> select();

        foreach ($search_detail_all as &$unit) {
            foreach ($all_dict_operate as $i) {
                if($unit['operate_code'] == $i['operate_code']){
                    $unit['operate_code'] = $i['operate_name'];
                }
            }

            foreach ($all_dict_resource as $i) {
                if($unit['rescode'] == $i['rescode']){
                    $unit['rescode'] = $i['resname'];
                }
            }
        }
        unset($unit); //取消引用

        $this->ajaxReturn($search_detail_all);
    }

 }